﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Net;

namespace gTray
{
    public partial class frmLogin : Form
    {
        private bool _DoLogin;
        public frmLogin()
        {
            InitializeComponent();
            _DoLogin = false;

            this.Text = "gTray - ";
        }
        /// <summary>
        /// Shows the login form and gets the credentials of the user.
        /// Return null if no login requested.
        /// </summary>
        /// <param name="owner">The owner form.</param>
        /// <returns></returns>
        public NetworkCredential ShowAndGetCredentials(IWin32Window owner)
        {
            this.Text += "Login";
            btnExit.Text = "Exit";

            if (SettingService.Instance.DoSaveCredentials == true)
            {
                NetworkCredential net = SettingService.Instance.GetAutoLoginCredential();
                cbUsername.Text = net.UserName;
                txtPassword.Text = net.Password;

                checkSaveUsername.Checked = true;
            }

            //Force collect to remove 'net' object (for added security)
            GC.Collect();

            return InternalShowAndGet(owner);
        }

        /// <summary>
        /// Shows the login form and gets the credentials of the user.
        /// Return null if there is no change requested.
        /// </summary>
        /// <param name="owner">The owner form.</param>
        /// <returns></returns>
        public NetworkCredential ShowAndEditCredentials(IWin32Window owner, NetworkCredential credentials)
        {
            this.Text += "Edit Login";
            btnExit.Text = "Cancel";

            cbUsername.Text = credentials.UserName;
            txtPassword.Text = credentials.Password;

            checkSaveUsername.Checked = SettingService.Instance.DoSaveCredentials;
            checkAutoLogin.Checked = SettingService.Instance.DoAutoLogin;

            return InternalShowAndGet(owner);
        }

        /// <summary>
        /// Internal extension to the Show and Get functions (allows for a bit of code aggregation)
        /// </summary>
        /// <param name="owner">The owner form.</param>
        /// <returns></returns>
        private NetworkCredential InternalShowAndGet(IWin32Window owner)
        {
            foreach (string str in SettingService.Instance.Usernames)
            {
                cbUsername.Items.Add(str);
            }
            if (cbUsername.Items.Count > 0)
                cbUsername.SelectedIndex = 0;

            this.ShowDialog(owner);

            if (_DoLogin)
            {
                if (checkSaveUsername.Checked == true)
                {
                    if (!SettingService.Instance.Usernames.Contains(cbUsername.Text))
                        SettingService.Instance.Usernames.Add(cbUsername.Text);
                }
                else
                {
                    if (SettingService.Instance.Usernames.Contains(cbUsername.Text))
                        SettingService.Instance.Usernames.Remove(cbUsername.Text);
                }

                if (checkAutoLogin.Checked == true)
                {
                    SettingService.Instance.DoAutoLogin = true;
                    SettingService.Instance.AddAutoLoginCredential(cbUsername.Text.ToLower(), txtPassword.Text.ToLower());
                }
                else
                {
                    SettingService.Instance.DoAutoLogin = false;
                }

                SettingService.Instance.Save();

                return new NetworkCredential(cbUsername.Text.ToLower(), txtPassword.Text.ToLower());
            }
            else
                return null;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (NetService.CheckCredentialsHTTP(cbUsername.Text.ToLower(), txtPassword.Text.ToLower())!= HttpStatusCode.OK)
            {
                MessageBox.Show("Login Failed. Check your username and password", "Login Failure");
            }
            else
            {
                _DoLogin = true;
                this.Close();
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            _DoLogin = false;
            this.Close();
        }

        private void checkAutoLogin_CheckedChanged(object sender, EventArgs e)
        {
            if (checkAutoLogin.Checked == true)
                checkSaveUsername.Checked = true;
        }

        private void checkSaveCredentials_CheckedChanged(object sender, EventArgs e)
        {
            if (checkSaveUsername.Checked == false)
                checkAutoLogin.Checked = false;
        }

        private void cbUsername_TextChanged(object sender, EventArgs e)
        {
            ToggleLoginButton();
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            ToggleLoginButton();
        }

        /// <summary>
        /// Toggles the login button.
        /// </summary>
        private void ToggleLoginButton()
        {
            if (txtPassword.Text != String.Empty && cbUsername.Text != String.Empty)

                btnLogin.Enabled = true;
            else
                btnLogin.Enabled = false;
        }
    }
}
